/*
 * Place.java
 * 
 * Created on 08-sep-2007, 19:55:34
 */

package swagenda.data;

import java.awt.Color;
import java.io.Serializable;
import java.text.Collator;
import java.util.TreeSet;

import javax.xml.namespace.QName;

import org.apache.abdera.model.Category;
import org.apache.abdera.model.Element;
import org.apache.abdera.model.Entry;
import org.apache.abdera.model.Link;
import org.apache.log4j.Logger;

/**
 * 
 * @author jorge
 */
public class Service implements Serializable, Comparable<Service>
{
	private static final long serialVersionUID = 20080218;
	transient Logger logger = Logger.getLogger(this.getClass());

	Person owner;
	public String name;
    public String phone = "no-phone";
    public String url;
    public String oosId;
    public String address;
    public String sdLocality;
    public String dLocality;
    public swagenda.data.Category locality;
    public String sAdminArea;
    public String adminArea;
    public String country;
    public String title;
    public String review;
    public TreeSet<swagenda.data.Category> sections = new TreeSet<swagenda.data.Category>();
    public TreeSet<swagenda.data.Category> tags = new TreeSet<swagenda.data.Category>();
    public TreeSet<Photo> photos = new TreeSet<Photo>();
    public Privacy privacy;
    public long latitude;
    public long longitude;

    public enum Privacy
    {
    	PUBLIC("public", new Color(121,255,77)), 
    	CONTACTS("contacts", new Color(255,255,61)), 
    	TRUSTEDCONTACTS("trustedContacts", new Color(255,158,61)), 
    	PRIVATE("private", new Color(255,61,61));
    	
    	public final String key;
    	public final Color color;

    	Privacy(String key, Color color)
    	{
    		this.key = key;
    		this.color = color;
    	}
    	public static Privacy eval(String term)
    	{
    		if (term.equals(PUBLIC.key)) return PUBLIC;
    		if (term.equals(CONTACTS.key)) return CONTACTS;
    		if (term.equals(TRUSTEDCONTACTS.key)) return TRUSTEDCONTACTS;
    		if (term.equals(PRIVATE.key)) return PRIVATE;
    		else return null;
    	}
    }
    
    public Service(Person owner, Entry entry)
    {
    	this.owner = owner;
    	name = entry.getTitle();
    	title = entry.getSummary();
    	review = entry.getContent();
    	String elName;
		for (Element el: entry.getElements())
		{
			elName = el.getQName().getLocalPart().trim();
			if(elName.equals("telephone")) phone = el.getText();
			else if (elName.equals("useraddress")) address = el.getText();
			else if (elName.equals("url")) url = el.getText();
			else if (elName.equals("id")) oosId = el.getText();
			else if (elName.equals("subdependentlocality")) sdLocality = el.getText();
			else if (elName.equals("dependentlocality")) dLocality = el.getText();
			else if (elName.equals("locality")) locality = addLocation(el.getText());
			// TODO: process georss
		}
		for (Category cat: entry.getCategories())
		{
			if (cat.getScheme()!=null && cat.getScheme().toString().equals("http://11870.com/api/v1/privacy"))
				privacy = Privacy.eval(cat.getTerm());
			else if (cat.getScheme()!=null && cat.getScheme().toString().equals("http://11870.com/api/v1/lists/jorge"))
				addSections(cat.getTerm().split("\n"));
			else if (cat.getScheme()!=null && cat.getScheme().toString().equals("http://11870.com/api/v1/tags/jorge"))
				addTags(cat.getTerm().split("\n"));
		}
		for (Link l: entry.getLinks())
		{
			if ("media".equals(l.getRel()))
			{
				logger.debug(l);
				for (QName a: l.getAttributes())
				{
					if ("image/jpg".equals(a)) photos.add(new Photo(l));
					// TODO: process videos
				}
				
			}
		}
    }
    
    private swagenda.data.Category addLocation(String locName)
	{
		swagenda.data.Category c = owner.getOrCreateLocation(locName);
		c.places.add(this);
		return c;
	}
    private void addSections(String[] names)
    {
    	for (String name: names)
    	{
    		swagenda.data.Category c = owner.getOrCreateSection(name);
    		sections.add(c);
    		c.places.add(this);
    	}
    }
    private void addTags(String[] names)
    {
    	for (String name: names)
    	{
    		swagenda.data.Category c = owner.getOrCreateTag(name);
    		tags.add(c);
    		c.places.add(this);
    	}
    }
    
    public Service(String name, String phone) {
        this.name = name;
        this.phone = phone;
    }

	@Override
	public boolean equals(Object o)
	{
		if (!(o instanceof Service)) return false;
		Service p = (Service) o;
		return oosId.equals(p.oosId);
	}

	public int compareTo(Service p)
	{
		if(oosId.equals(p.oosId)) return 0;
		return Collator.getInstance().compare(name.toLowerCase(), p.name.toLowerCase());
	}
}
